package com.itheima;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.pojo.Goods;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

/**
 * 请求体查询-基本查询
 * 1.查询所有
 * 2.match查询，带分词器的查询
 * 3.multi_match查询，查询多个字段
 * 4.term查询，关键词精确匹配
 * 5.terms查询，多个关键词精确匹配
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo04BaseSearch {

    //注入高级客户端对象，用来操作es
    @Autowired
    private RestHighLevelClient client;

    /**
     * 查询所有
     * 1.创建请求对象：查询所有
     *    设置索引库name
     *    设置类型type
     * 2.创建查询请求体构建器
     *    设置请求体
     * 3.客户端发送请求，获取响应对象
     * 4.打印响应结果
     */
    @Test
    public void matchAllQuery() throws IOException {
        //1.创建请求对象：查询所有
        SearchRequest request = new SearchRequest();
        request.types("_doc");
        request.indices("heima");
        //2.创建查询请求体构建器
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        //第一个查询：设置查询方式：matchAll
        //sourceBuilder.query(QueryBuilders.matchAllQuery());

        //第二个查询：设置查询方式：match：带分词器的查询，查询条件分词后查询
        /**
         * 参数：需要查询字段，查询条件
         */
        //sourceBuilder.query(QueryBuilders.matchQuery("title","小米手机"));
        //operator(Operator.AND :设置match查询分词后的关键词的关联关系，默认or
        //sourceBuilder.query(QueryBuilders.matchQuery("title","小米手机").operator(Operator.AND));

        //第三个查询：ulti_match查询，查询多个字段
        //sourceBuilder.query(QueryBuilders.multiMatchQuery("小米","title","subtitle"));

        //第四个擦哈寻：term查询，关键词精确匹配
        //sourceBuilder.query(QueryBuilders.termQuery("title","小米手机"));

        //第五个查询：terms查询，多个关键词精确匹配
        sourceBuilder.query(QueryBuilders.termsQuery("price","2699.0","4288.0"));

        //设置请求体
        request.source(sourceBuilder);
        //3.客户端发送请求，获取响应对象
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //4.打印响应结果
        SearchHits hits = response.getHits();
        System.out.println("took::"+response.getTook());
        System.out.println("time_out::"+response.isTimedOut());
        System.out.println("total::"+hits.getTotalHits());
        System.out.println("max_score::"+hits.getMaxScore());
        System.out.println("hits::::>>");
        for (SearchHit hit : hits) {
            String sourceAsString = hit.getSourceAsString();
            System.out.println(sourceAsString);
        }
        System.out.println("<<::::");
    }


}